package com.beadhouse.nurrecord.web;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.beadhouse.common.dto.JSONResponse;
import com.beadhouse.common.dto.QueryResult;
import com.beadhouse.common.exeption.BeadHouseException;
import com.beadhouse.entity.CustomerAndNurcontentEntity;
import com.beadhouse.entity.NurContentEntity;
import com.beadhouse.entity.NurLevelAndNurContentEntity;
import com.beadhouse.nurcontent.service.NurContentService;
import com.beadhouse.nurlevel.dao.NurlevelContentDao;
import com.beadhouse.nurlevel.service.NurLevelContentService;
import com.beadhouse.nurlevel.support.service.NurLevelContentServiceImpl;
import com.beadhouse.nurrecord.ao.NursingProjectAo;
import com.beadhouse.nurrecord.ao.PerSonalNursingAo;
import com.beadhouse.nurrecord.service.NursingProjectService;
import com.beadhouse.nurrecord.support.service.NursingProjectServiceImpl;

/**
 * @author 刘聪
 */

@Controller
@RequestMapping("/nurrecord")
public class NursingProjectServlet {

	@Autowired
	NursingProjectService npsi;
	@Autowired
	NurLevelContentService nlcs;
	@ResponseBody
	@RequestMapping("/nurlevel/list.do")
	public JSONResponse nurContentList(Integer nurLevelId) throws Exception {
		if(nurLevelId==null) {
			throw new BeadHouseException("护理级别为空");
		}
		QueryResult<NurLevelAndNurContentEntity> list = nlcs.selectNurLevelContent(nurLevelId);
		return JSONResponse.ok(list);
	}
	@RequestMapping("/projectList.do")
	public ModelAndView selectPersonalNursing(PerSonalNursingAo ao) {
		if (ao.getKey() != null) {
			ao.setKey("%" + ao.getKey() + "%");
		}
		if (ao.getPageNo() == null) {
			ao.setPageNo("1");
		}
		if (ao.getPageSize() == null) {
			ao.setPageSize("5");
		}

		QueryResult<CustomerAndNurcontentEntity> dto;
		try {
			dto = npsi.selectPersonalNursingProject(ao);
			JSONResponse result = new JSONResponse();
			result.setCode("ok");
			result.setData(dto);

			ModelAndView mav = new ModelAndView();
			mav.setViewName("nurrecord/nurtoday");
			mav.addObject("date", result);
			return mav;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	@ResponseBody
	@GetMapping("/api/projectAdd")
	public JSONResponse add(NursingProjectAo ao) {
		try {
			String time = ao.getStartTime();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String data = sdf.format(new Date());
			
			ao.setStartTime(data+" "+time);
			System.out.println(ao);
			npsi.addPersonalNursingProject(ao);
			return JSONResponse.ok();
		}catch (BeadHouseException e) {
			return JSONResponse.fail(e);
		} catch (Exception e) {
			e.printStackTrace();
			return JSONResponse.fail(e);
		}
	}

	@ResponseBody
	@GetMapping("/api/projectDelete")
	public JSONResponse delete(String customerId, String nurContentId){
		try {
			npsi.deletePersonalNursingProject(customerId, nurContentId);
			return JSONResponse.ok();
		} catch (BeadHouseException e) {
			return JSONResponse.fail(e);
		} catch (Exception e) {
			return JSONResponse.fail(e);
		}
	}

	@ResponseBody
	@GetMapping("/api/projectSet")
	public JSONResponse SetRecord(String customerId, String nurContentId) {
		try {
			npsi.SetRecord(customerId, nurContentId);
			return JSONResponse.ok();
		}  catch (BeadHouseException e) {
			return JSONResponse.fail(e);
		} catch (Exception e) {
			return JSONResponse.fail(e);
		}
	}
}
